<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>generateWhile</title>
</head>

<body>
    <div>打开浏览器控制台查看结果</div>
    <script>
        const generateWhile = function* (seed, condition, next) {
            let val = seed;
            let nextSeed = null;
            while (condition(val)) {
                nextSeed = yield val;
                val = next(val, nextSeed);
            }
            return val;
        };
        console.log([
            ...generateWhile(
                1,
                v => v <= 5,
                v => ++v
            )
        ]);
    </script>
</body>

</html>